package com.example.zhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhxy.model.LoginInfo;
import com.example.zhxy.model.Student;
import com.example.zhxy.service.StudentService;
import com.example.zhxy.mapper.StudentMapper;
import com.example.zhxy.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
* @author fzykd
* @description 针对表【tb_student】的数据库操作Service实现
* @createDate 2023-03-13 21:26:35
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{

    @Override
    public Student login(LoginInfo loginInfo) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", loginInfo.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginInfo.getPassword()));

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public Student getStudentById(Long userId) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        return baseMapper.selectById(queryWrapper);
    }


    //学生管理分页查询

    @Override
    public IPage<Student> stuPage(Page<Student> page, String name, String clazzName) {
        //条件构造器
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        //
        queryWrapper.like(!StringUtils.isEmpty(name), Student::getName,name);
        queryWrapper.eq(!StringUtils.isEmpty(clazzName), Student::getClazzName,clazzName);

        //
        queryWrapper.orderByAsc(Student::getId);

        return baseMapper.selectPage(page,queryWrapper);

    }
}




